package tt.dz.egobus.servicemanager.controller.ordertag;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import tt.dz.egobus.api.ResponseBack;
import tt.dz.egobus.api.page.Filter;
import tt.dz.egobus.api.page.Page;
import tt.dz.egobus.api.page.Pageable;
import tt.dz.egobus.bean.CodeStatus;
import tt.dz.egobus.bean.comment.TagBack;
import tt.dz.egobus.bean.comment.TagRequest;
import tt.dz.egobus.servicemanager.base.controller.CommonBinderController;
import tt.dz.egobus.servicemanager.bean.PagingationStart;
import tt.dz.egobus.servicemanager.core.exception.BusinessException;
import tt.dz.egobus.servicemanager.service.OrderTagManagerService;
import tt.dz.egobus.servicemanager.util.DubboManagerPagableUtils;
import tt.dz.egobus.servicemanager.util.MapGetUtils;
import tt.dz.egobus.servicemanager.util.PojoConvertVoUtils;
import tt.dz.egobus.servicemanager.vo.JsonBack;
import tt.dz.egobus.servicemanager.vo.OrderTagViewVo;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static tt.dz.egobus.servicemanager.bean.ServiceManagerCodeStatus.PARAM_ENTITY_ERROR;
import static tt.dz.egobus.servicemanager.bean.ServiceManagerCodeStatus.SYSTEM_CODE_ERROR;

/**
 * @author Anson
 * @date 17-8-11
 * @name 订单标签管理界面通道
 * @since 1.0.6
 */
@RestController
@RequestMapping("/order/ordertag")
public class OrderTagController extends CommonBinderController {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderTagController.class);


    @Autowired
    @Qualifier("dubboOrderTagManagerServiceImpl")
    private OrderTagManagerService orderTagManagerService;

    /**
     * @author Anson
     * @date 17-8-11
     * @name 分页查询
     * @since 1.0.6
     */
    @RequestMapping(value = "/pageStart", method = RequestMethod.POST)
    public JsonBack<PagingationStart<OrderTagViewVo>> pagationStart(@RequestParam Map<String, Object> param) {
            JsonBack<PagingationStart<OrderTagViewVo>> back = null;
        try {
            Pageable pageable = new Pageable();
            int start = MapGetUtils.getAndRemoveDigits(param, "start");
            int maxResult = MapGetUtils.getAndRemoveDigits(param, "limit");
            int pageNumber = start / maxResult + 1;
            param = this.convertParams(param);
            pageable.setPageNumber(pageNumber);
            pageable.setPageSize(maxResult);
            DubboManagerPagableUtils utils = new DubboManagerPagableUtils();
            List<Filter> filters = utils.adaptcher(param);
            pageable.setFilters(filters);
            ResponseBack<Page<TagBack>> staffBackResponse = orderTagManagerService.queryPage(pageable);
            if (staffBackResponse.isSuccess()) {
                Page<TagBack> staffBacks = staffBackResponse.getResult();
                PagingationStart<OrderTagViewVo> pagingationStart = new PagingationStart<OrderTagViewVo>();
                if (staffBacks != null) {
                    List<TagBack> result = staffBacks.getContent();
                    List<OrderTagViewVo> viewVos = PojoConvertVoUtils.convertTagBackToViews(result);
                    pagingationStart.setPageSize(maxResult);
                    pagingationStart.setList(viewVos);
                    pagingationStart.setAllRow(Long.valueOf(staffBacks.getTotal()).intValue());
                }
                back = JsonBack.success(pagingationStart);
            } else {
                back = JsonBack.instance(staffBackResponse, null);
            }
        } catch (BusinessException e) {
            back = JsonBack.error(e);
            LOGGER.error("[pageStart]分页查询", e);
        } catch (Exception e) {
            back = JsonBack.error(SYSTEM_CODE_ERROR);
            LOGGER.error("[pageStart]分页查询", e);
        }
        return back;
    }
/*
    *//**
     * @author Anson
     * @date 17-8-11
     * @name 判断某个身份证是否已经存在记录
     * @since 1.0.6
     *//*
    @RequestMapping("/isExistsCardNo")
    public JsonBack<Boolean> judgeStaffExists(@RequestParam("idCard") String idCard) {
        JsonBack<Boolean> back = null;
        try {
            ResponseBack<Boolean> isExits = orderTagManagerService.judgeStaffExists(idCard);
            if (isExits.isSuccess()) {
                back = JsonBack.success(isExits.getResult());
            } else {
                back = JsonBack.instance(isExits, null);
            }
        } catch (BusinessException e) {
            back = JsonBack.error(e);
            LOGGER.error("新增参数", e);
        } catch (Exception e) {
            back = JsonBack.error(SYSTEM_CODE_ERROR);
            LOGGER.error("新增参数", e);
        }
        return back;
    }*/

    /**
     * @author Anson
     * @date 17-8-11
     * @name 新增
     * @since 1.0.6
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonBack<OrderTagViewVo> insert(@ModelAttribute @Valid OrderTagViewVo v, BindingResult result) {
        JsonBack<OrderTagViewVo> back = null;
        try {
            if (result.hasErrors()) {
                //如果没有通过,跳转提示
                JsonBack.throwErrorException(result);
            }
            TagRequest tagRequest = new TagRequest();
            tagRequest.setStarLevel(v.getStarLevel());
            tagRequest.setValue(v.getValue());
            ResponseBack<TagBack> staffBack = orderTagManagerService.addTag(tagRequest);
            if (staffBack.isSuccess()) {
                TagBack result1 = staffBack.getResult();
                OrderTagViewVo vo = new OrderTagViewVo();
                back = JsonBack.success(result1 == null ? vo : vo.view(result1));
            } else {
                back = JsonBack.instance(staffBack, null);
            }
        } catch (BusinessException e) {
            back = JsonBack.error(e);
            LOGGER.error("新增参数", e);
        } catch (Exception e) {
            back = JsonBack.error(SYSTEM_CODE_ERROR);
            LOGGER.error("新增参数", e);
        }
        return back;
    }

    /**
     * @author Anson
     * @date 17-8-11
     * @name 更新
     * @since 1.0.6
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonBack<OrderTagViewVo> update(@ModelAttribute @Valid OrderTagViewVo v, BindingResult result) {
        JsonBack<OrderTagViewVo> back = null;
        try {
            if (result.hasErrors()) {
                //如果没有通过,跳转提示
                JsonBack.throwErrorException(result);
            }
            if (StringUtils.isBlank(v.getId())) {
                //主键为空的时候
                back = JsonBack.error(PARAM_ENTITY_ERROR);
            } else {
                ResponseBack<TagBack> staffBack = orderTagManagerService.updateTag(v.getId(), v.getValue());
                if (staffBack.isSuccess()) {
                    OrderTagViewVo vo = new OrderTagViewVo();
                    TagBack result1 = staffBack.getResult();
                    back = JsonBack.success(result1 == null ? vo : vo.view(result1));
                } else {
                    back = JsonBack.instance(staffBack, null);
                }
            }
        } catch (BusinessException e) {
            back = JsonBack.error(e);
            LOGGER.error("修改参数", e);
        } catch (Exception e) {
            back = JsonBack.error(SYSTEM_CODE_ERROR);
            LOGGER.error("修改参数", e);
        }
        return back;
    }

    /**
     * @author Anson
     * @date 17-8-11
     * @name 删除
     * @since 1.0.6
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public JsonBack<Boolean> delete(@RequestParam("id") String id) {
        JsonBack<Boolean> back = null;
        try {
            final ResponseBack<Boolean> isDelete = orderTagManagerService.deleteTag(id);
            if (!isDelete.isSuccess()) {
                back = JsonBack.instance(new CodeStatus() {
                    @Override
                    public String getMsg() {
                        return isDelete.getMsg();
                    }

                    @Override
                    public int getCode() {
                        return isDelete.getCode();
                    }
                }, false);
            } else if (!isDelete.getResult()) {
                back = JsonBack.success(false);
            } else {
                back = JsonBack.success(true);
            }
        } catch (BusinessException e) {
            back = JsonBack.error(e);
            LOGGER.error("删除参数", e);
        } catch (Exception e) {
            back = JsonBack.error(SYSTEM_CODE_ERROR);
            LOGGER.error("删除参数", e);
        }
        return back;
    }
}
